package lanou.giftsay_01.tools;

import android.os.Handler;
import android.os.Looper;

import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.assit.QueryBuilder;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


/**
 * Created by dllo on 16/8/24.
 * 数据库的操作类
 * 使用单例模式,因为LiteOrm要求的
 * 其他数据库框架 GreenDao , OrmLite
 */
public class DBTool {
    public static DBTool singleTon;
    //liteOrm 对象,增删改查方法 都操作此对象
    private LiteOrm mliteOrm;
    //Handler 用来做线程且换,为了在主线程来执行回调,返回数据
    private Handler handler ;
    //线程池
    ExecutorService threadPool = Executors.newFixedThreadPool(5);
    public static DBTool getInstance() {
        if (singleTon == null) {
            synchronized (DBTool.class) {
                if (singleTon == null) {
                    singleTon = new DBTool();

                }
            }
        }
        return singleTon;
    }


    private DBTool() {

        /**
         * 初始化liteOrm对象
         * 使用MyApp里的全局Context
         * 第二个参数是 数据库的名字
         */
        mliteOrm = LiteOrm.newCascadeInstance(MyApp.getContext() , "MyDataBase.db");
        /**
         * 初始化handler
         */
        /**
         * handler的构造方法 参数如果传
         * Looper.getMainLooper()
         * 代表这个Handler 一定会回调主线程
         * 属于严谨的写法
         */
        handler = new Handler(Looper.getMainLooper());
    }
    public void insertCollectionBean(final NewBean collectionBean){
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                mliteOrm.insert(collectionBean);
            }
        });

    }
    public void deleteColletionBean(final String where,final String s){

        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                QueryBuilder<NewBean> builder = new QueryBuilder<NewBean>(NewBean.class);
                builder.where(where ,s);//加入条件
                ArrayList<NewBean> arrayList = mliteOrm.query(builder);
                mliteOrm.delete(arrayList);

            }
        });
    }
    public void queryAllCollection (final QueryListener queryListener){
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                /**
                 * 在子线程中查询数据库,查询结果是students
                 */
                ArrayList<NewBean> arrayList = mliteOrm.query(NewBean.class);
                /**
                 * 查询完成后使用Handler 在主线程回调接口对象
                 * 把数据传递出去
                 * handler通过post方法 接受一个Runnable()对象
                 * 会使该对象的run()方法 在主线程中执行
                 */
        handler.post(new HandlerRunnable(arrayList , queryListener));
            }
        });
    }
    public void querySingleCollection(final String where,final String s, final QueryListener queryListener){
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                QueryBuilder<NewBean> builder = new QueryBuilder<NewBean>(NewBean.class);
                builder.where(where ,s);//加入条件
                ArrayList<NewBean> arrayList = mliteOrm.query(builder);
                handler.post(new HandlerRunnable(arrayList ,queryListener));
            }
        });
    }
    class  HandlerRunnable implements  Runnable{
    ArrayList<NewBean> arrayList ;
        QueryListener queryListener;
        public HandlerRunnable(ArrayList<NewBean> arrayList, QueryListener queryListener) {
            this.arrayList = arrayList;
            this.queryListener = queryListener;
        }
        @Override
        public void run() {
            queryListener.onQueryComplete(arrayList);
        }
    }
    public interface  QueryListener{

        void onQueryComplete(ArrayList<NewBean> arrayList);
    }
    public void deleteTowColletionBean(final String where , final String where2 ,final String s ,final String a){

        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                QueryBuilder<NewBean> builder = new QueryBuilder<NewBean>(NewBean.class);
                builder.where(where , s);
                builder.whereAppendAnd().whereAppend(where2 ,a) ;//加入条件
                ArrayList<NewBean> arrayList = mliteOrm.query(builder);
                mliteOrm.delete(arrayList);

            }
        });
    }
    public void queryTwoCollection(final String where, final String where2 ,final String s ,final String a, final QueryListener queryListener){
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                QueryBuilder<NewBean> builder = new QueryBuilder<NewBean>(NewBean.class);
                builder.where(where , s);
                builder.whereAppendAnd().whereAppend(where2 , a);//加入条件
                ArrayList<NewBean> arrayList = mliteOrm.query(builder);
                handler.post(new HandlerRunnable(arrayList ,queryListener));
            }
        });
    }

}